package com.demo.my.Dao;

import com.demo.my.Entity.Department;
import com.demo.my.Entity.Employee;
import com.demo.my.Entity.Position;
import com.demo.my.Util.BaseDAO;

import com.demo.my.Entity.Department;
import com.demo.my.Util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class DepartmentDao {
    private static BaseDAO dao = new BaseDAO() ;
    public Department getEntityById(Integer id) {
        String sql = "select * from department where department_number=?";
        Object[] objs = {id};
        try {
            Department one = dao.getOne(sql, new Department(), objs);
            return one;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }
    //高和召
    public Department getDepartment(Integer departmentNumber) throws SQLException {
        Connection connection = DBUtil.getConnection();
        PreparedStatement ps = connection.prepareStatement("select * from department where department_number=?");
        ps.setObject(1,departmentNumber);
        ResultSet rs = ps.executeQuery();
        rs.next();
        int id = rs.getInt(1);
        int deNumber = rs.getInt(2);
        String name = rs.getString(3);
        String manager = rs.getString(4);
        String telephone = rs.getString(5);
        String address = rs.getString(6);
        String notes = rs.getString(7);
        DBUtil.close(rs,ps,connection);
        return new Department(id,deNumber,name,manager,telephone,address,notes);
    }

    public List<Department> getDepList(String pageNo) throws Exception {
        String sql="select * from department limit ?,?";
        int num = (Integer.parseInt(pageNo) - 1) * 8;
        Object[] obj={num,8};
        return   dao.getList(sql,new Department(),obj);
    }
    public List<Department> getDepList() throws Exception {
        String sql="select * from department ";
        Object[] obj={};
        return   dao.getList(sql,new Department(),obj);
    }

    public int getDepListCount() throws Exception {
        String sql="select count(*) from department";

       return dao.getCount(sql,new Object[0]);
    }

    public void addDepartment(Department department) throws Exception {
        String sql="insert into department values(null,?,?,?,?,?,?)";
        Object[] obj={department.getDepartmentNumber(),department.getName(),department.getManager(),
                department.getTelephone(),department.getAddress(),department.getAddress()};
        dao.addOrupdOrdel(sql,obj);
    }

    public Department getDepById(String id) throws Exception {
        String sql="select * from department where id=?";
        Object[] obj={id};
       return dao.getOne(sql,new Department(),obj);
    }

    public void updateDepartment(Department dep) throws Exception {
        String sql="update department set department_number=?,name=?,manager=?,telephone=?,address=?,notes=? where id=?";
        Object[] obj={dep.getDepartmentNumber(),dep.getName(),dep.getManager(),
                dep.getTelephone(),dep.getAddress(),dep.getNotes(),dep.getId()};
        dao.addOrupdOrdel(sql,obj);
    }

    public void deleteDepartment(String id) throws Exception {
        String sql="delete from department where id=?";
        Object[] obj={id};
        dao.addOrupdOrdel(sql,obj);
    }

    public int getMaxNumber() throws Exception {
        String sql="select MAX(department_number) from department";
       return dao.getCount(sql,new Object[0]);
    }
}
